<?php

namespace app\admin\model;

use think\Model;

class CallRecord extends Model
{
    // 表名
    protected $name = 'call_record';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    
    // 追加属性
    protected $append = [
        'type_text'
    ];
    

    
    public function getTypeList()
    {
        return ['callin' => __('Type callin'),'callout' => __('Type callout'),'missed' => __('Type missed'),'answered' => __('Type answered'),'failed' => __('Type failed')];
    }     


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['type'];
        $valueArr = explode(',', $value);
        $list = $this->getTypeList();
        return implode(',', array_intersect_key($list, array_flip($valueArr)));
    }

    protected function setTypeAttr($value)
    {
        return is_array($value) ? implode(',', $value) : $value;
    }


    public function admin()
    {
        return $this->belongsTo('Admin', 'admin_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

}
